// page.js
Page({
  data: {
      detail:{},
      id:'',
       current:0,
       isLoading: true ,
       imageUrls:[],
       buy:'',
       avatarUrl:'',
       nickName:''
    },
    onLoad: function(options) {
      // 显示加载提示框
      // 当页面加载时，options 参数会包含 URL 中传递的查询字符串参数
      this.setData({
        id: options.id
      });
      console.log("Received item ID:", this.data.id);
      this.fetchItemDetails(this.data.id)
  
    },

    fetchItemDetails: function(itemId) {
      var that = this;
      wx.showLoading({
        title: '加载中',
        mask: true  // 防止触摸穿透
      });
    
      // 实现根据 itemId 获取详细信息的逻辑
      // 这里是伪代码，需要根据实际情况实现
      wx.request({
        url: 'https://9p47q51245.goho.co/index_detail' ,
        method: 'POST',
        data: {
          id: itemId
        },
        header: {
          'Content-Type': 'application/json'
        },
        success: function(res) {
          // 处理获取到的数据
          console.log("details:", res.data);
          if (res.data && Object.keys(res.data).length > 0) {
            const imgProperties = [res.data.img1, res.data.img2, res.data.img3, res.data.img4, res.data.img5, res.data.img6, res.data.img7, res.data.img8, res.data.img9];
            const nonEmptyImages = imgProperties.filter(img => img !== null && img !== undefined && img !== '');
            that.setData({
              detail: res.data,
              isLoading: false, // 数据加载完成，显示页面
              imageUrls: [...that.data.imageUrls, ...nonEmptyImages],
              buy:res.data.buy
            });
            wx.request({
              url: 'https://9p47q51245.goho.co/User_find_detail' ,
              method: 'POST',
              data: {
                id: that.data.detail.author
              },
              header: {
                'Content-Type': 'application/json'
              },
              success:function(res){
                that.setData({
                  avatarUrl:res.data.avatarUrl,
                  nickName:res.data.nickName
                })
              },
              fail:function(res){
                wx.showToast({
                  title: '加载失败',
                  icon: 'none'
                });
              }
            })
            wx.hideLoading();
          } else {
            // 数据为空时，可以设置isLoading为false并处理显示空状态
            that.setData({
              isLoading: false
            });
            wx.showToast({
              title: '无数据',
              icon: 'none'
            });
          }
        },
        fail: function() {
          // 处理请求失败的情况
          wx.showToast({
            title: '加载失败',
            icon: 'none'
          });
        },
        complete: function() {
          // 请求完成后隐藏加载提示框
          wx.hideLoading();
        }
      });
    },
    
onSwiperChange : function(e) {
      // 图片滑动时更新当前索引
      this.setData({
        current: e.detail.current
      });
    },

    handleContact: function() {
      // 沟通逻辑
      var openId = wx.getStorageSync('openId');
      var author=this.data.detail.buyer_id
      if (!openId) {
        // 处理openId不存在的情况，例如提示用户登录
        wx.showToast({
          title: '请先登录',
          icon: 'none'
        });
      
        return;
      }
      wx.navigateTo({
        url: '/pages/communication/communication?openId=' + openId + '&detailId=' +  author,
        success: function(res) {
          // 成功跳转后的回调
        },
        fail: function(res) {
          // 跳转失败后的回调
        }
      });
      wx.showToast({
        title: '沟通中',
        icon: 'success'
      });
    },
  handleBuy: function(e) {
    var that = this;
    wx.showModal({
      title: '确认下架？',
      content: '是否确认下架商品？',
      success: function (res) {
        if (res.confirm) {
          // 用户点击了“是”，执行取消交易逻辑
          wx.request({
            url: 'https://9p47q51245.goho.co/index_detail',
            method: 'POST',
            data: {
              id: that.data.id,
            },
            header: {
              'Content-Type': 'application/json'
            },
            success: function(res) {
              // 处理获取到的数据
              console.log(res.data)
              if (res.data.buy == 1) {
                wx.showToast({
                  title: '下架商品成功',
                  icon: 'success'
                });
  
                var openId = wx.getStorageSync('openId');
                that.setData({
                  buy: 2
                });
  
                wx.request({
                  url: 'https://9p47q51245.goho.co/Object_buy_and_sell',
                  method: 'POST',
                  data: {
                    openId: openId,
                    id: that.data.id,
                    buy: that.data.buy
                  },
                  header: {
                    'Content-Type': 'application/json'
                  },
                  success: function(res) {
                    console.log(res);
                  },
                  fail: function() {
                    // 处理请求失败的情况
                    wx.showToast({
                      title: '请求失败',
                      icon: 'none'
                    });
                  }
                });
              }
            },
            fail: function() {
              // 处理请求失败的情况
              wx.showToast({
                title: '请求失败',
                icon: 'none'
              });
            }
          });
        } else if (res.cancel) {
          // 用户点击了“否”，不做任何操作
          console.log('用户点击了取消');
        }
      }
    });
  },
  
  qBuy1: function(e) {
    var that = this;
    wx.showModal({
      title: '确认取消交易',
      content: '是否确认取消交易？',
      success: function (res) {
        if (res.confirm) {
          // 用户点击了“是”，执行取消交易逻辑
          wx.request({
            url: 'https://9p47q51245.goho.co/index_detail',
            method: 'POST',
            data: {
              id: that.data.id
            },
            header: {
              'Content-Type': 'application/json'
            },
            success: function(res) {
              // 处理获取到的数据
              if (res.data.buy == 0) {
                wx.showToast({
                  title: '取消交易成功',
                  icon: 'success'
                });
  
                var openId = wx.getStorageSync('openId');
                that.setData({
                  buy: 1
                });
  
                wx.request({
                  url: 'https://9p47q51245.goho.co/Object_buy_and_sell',
                  method: 'POST',
                  data: {
                    openId: openId,
                    id: that.data.id,
                    buy: that.data.buy
                  },
                  header: {
                    'Content-Type': 'application/json'
                  },
                  success: function(res) {
                    console.log(res);
                  },
                  fail: function() {
                    // 处理请求失败的情况
                    wx.showToast({
                      title: '请求失败',
                      icon: 'none'
                    });
                  }
                });
              }
            },
            fail: function() {
              // 处理请求失败的情况
              wx.showToast({
                title: '请求失败',
                icon: 'none'
              });
            }
          });
        } else if (res.cancel) {
          // 用户点击了“否”，不做任何操作
          console.log('用户点击了取消');
        }
      }
    });
  },
  
  qBuy3: function(e) {
    var that = this;
    wx.showModal({
      title: '确认交易',
      content: '是否确认完成交易？',
      success: function (res) {
        if (res.confirm) {
          // 用户点击了“是”，执行交易逻辑
          wx.request({
            url: 'https://9p47q51245.goho.co/index_detail',
            method: 'POST',
            data: {
              id: that.data.id
            },
            header: {
              'Content-Type': 'application/json'
            },
            success: function(res) {
              // 处理获取到的数据
              if (res.data.buy == 0) {
                wx.showToast({
                  title: '成功完成交易',
                  icon: 'success'
                });
  
                var openId = wx.getStorageSync('openId');
                that.setData({
                  buy: 3
                });
  
                wx.request({
                  url: 'https://9p47q51245.goho.co/Object_buy_and_sell',
                  method: 'POST',
                  data: {
                    openId: openId,
                    id: that.data.id,
                    buy: that.data.buy
                  },
                  header: {
                    'Content-Type': 'application/json'
                  },
                  success: function(res) {
                    console.log(res);
                  },
                  fail: function() {
                    // 处理请求失败的情况
                    wx.showToast({
                      title: '请求失败',
                      icon: 'none'
                    });
                  }
                });
              }
            },
            fail: function() {
              // 处理请求失败的情况
              wx.showToast({
                title: '请求失败',
                icon: 'none'
              });
            }
          });
        } else if (res.cancel) {
          // 用户点击了“否”，不做任何操作
          console.log('用户点击了取消');
        }
      }
    });
  },
  


  back:function(){
    wx.navigateBack({
      delta: 1  // 返回的页面数，默认为1
    });
  },
  previewImages: function(e) {
    // 获取点击图片的索引
    var index = e.currentTarget.dataset.index;
    // 获取图片数组
    var imageUrls = this.data.imageUrls;
    // 设置当前预览的图片
    var currentImageUrl = imageUrls[index];

    wx.previewImage({
      current: currentImageUrl, // 当前显示图片的http链接
      urls: imageUrls // 需要预览的图片http链接列表
    });
  }
});
